// Written by Steve Andersen, copyright (c) 2007 ONE/Northwest
// This program is released under the GNU General Public License. http://www.gnu.org/licenses/
public class Test_Project_Task_Hours_Change {
	//create an opp
	//create a task log
	//check for values
	
	static testMethod void TestProjectTaskCreated() {	 
		Id OppId;
		//create first contact
		Opportunity firstOpp = new Opportunity (Name='test opp',StageName='Prospecting',CloseDate=System.today());
		insert firstOpp;
		OppId = firstOpp.id;
		
		ONEN_Project_Task__c firstTask = new ONEN_Project_Task__c (
			Name='Implementation',
			Opportunity__c=OppId,
			Expected_Hours__c=6
		);
		insert firstTask;
		Opportunity[] OppAfterTask = [select Scheduled_Hours__c from Opportunity where Id =:OppId];
		System.assertEquals(6,OppAfterTask[0].Scheduled_Hours__c);
		
		ONEN_Project_Task_Log__c firstTaskLog = new ONEN_Project_Task_Log__c (
			Name='some work',
			Opportunity__c=OppId,
			ONEN_Project_Task__c=firstTask.id,
			Task_Log_Hours__c=4
		);
		insert firstTaskLog;
		
		Opportunity[] OppAfterTaskLog = [select Worked_Hours__c from Opportunity where Id =:OppId];
		System.assertEquals(4,OppAfterTaskLog[0].Worked_Hours__c);
		//a household should be automatically created	
		
		firstTask.Expected_Hours__c=8;
		update firstTask;
		
		Opportunity[] OppAfterTaskChange = [select Scheduled_Hours__c from Opportunity where Id =:OppId];
		System.assertEquals(8,OppAfterTaskChange[0].Scheduled_Hours__c);
		
		ONEN_Project_Task_Log__c secondTaskLog = new ONEN_Project_Task_Log__c (
			Name='some work 2',
			Opportunity__c=OppId,
			ONEN_Project_Task__c=firstTask.id,
			Task_Log_Hours__c=9
		);
		insert secondTaskLog;
		
		Opportunity[] OppAfterTaskAdd = [select Worked_Hours__c from Opportunity where Id =:OppId];
		System.assertEquals(13,OppAfterTaskAdd[0].Worked_Hours__c);
		
		ONEN_Project_Task_Log__c thirdTaskLog = new ONEN_Project_Task_Log__c (
			Name='some work 3',
			Opportunity__c=OppId,
			ONEN_Project_Task__c=firstTask.id,
			Task_Log_Hours__c=null
		);
		insert thirdTaskLog;
		
		Opportunity[] OppAfterTaskAddNull = [select Worked_Hours__c from Opportunity where Id =:OppId];
		System.assertEquals(13,OppAfterTaskAddNull[0].Worked_Hours__c);
		
		//delete firstTaskLog;
		delete firstTask;
		
		Opportunity[] OppAfterTaskDelete = [select Scheduled_Hours__c from Opportunity where Id =:OppId];
		System.assertEquals(0,OppAfterTaskDelete[0].Scheduled_Hours__c);
		
	}
	
}